﻿// -----------------------------------------------------------------------
// <copyright file="IMG_Resize.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImageProcessing.IMG_Processor
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class IMG_Resize
    {
        /// <summary>
        /// bilinear resize function
        /// </summary>
        /// <param name="img_OldBuffer">old image buffer</param>
        /// <param name="img_Width">x resize fraction </param>
        /// <param name="img_Height">y resize fraction</param>
        /// <returns>new buffer of the resi</returns>
        public static Entities.IMG_Image IMG_BiLinearResize(Entities.IMG_Image img_OldBuffer, int img_Width, int img_Height)
        {
            try
            {
                int img_OW = img_OldBuffer.Width;
                int img_OH = img_OldBuffer.Height;
                int img_NW = img_Width;
                int img_NH = img_Height;
                int img_X1, img_X2, img_Y1, img_Y2;

                float img_WRatio = img_OW / (float)img_NW;
                float img_HRatio = img_OH / (float)img_NH;

                float img_OldX, img_OldY, img_XFraction, img_YFraction;

                Entities.Pixel img_P1, img_P2, img_P3, img_P4, img_Z1, img_Z2, img_NewPixel;
                Entities.IMG_Image img_NewBuffer = new Entities.IMG_Image(img_NW, img_NH);

                for (int i = 0; i < img_NW; i++)
                {
                    for (int j = 0; j < img_NH; j++)
                    {
                        img_OldX = i * img_WRatio;
                        img_OldY = j * img_HRatio;
                        img_X1 = (int)Math.Floor(img_OldX);
                        if (img_X1 == img_OW - 1)
                        {
                            img_X2 = img_X1;
                        }
                        else
                        {
                            img_X2 = img_X1 + 1;
                        }

                        img_Y1 = (int)Math.Floor(img_OldY);
                        if (img_Y1 == img_OH - 1)
                        {
                            img_Y2 = img_Y1;
                        }
                        else
                        {
                            img_Y2 = img_Y1 + 1;
                        }

                        img_P1 = img_OldBuffer[img_Y1, img_X1];
                        img_P2 = img_OldBuffer[img_Y1, img_X2];
                        img_P3 = img_OldBuffer[img_Y2, img_X1];
                        img_P4 = img_OldBuffer[img_Y2, img_X2];
                        img_XFraction = img_OldX - img_X1;
                        img_YFraction = img_OldY - img_Y1;
                        img_Z1 = new Entities.Pixel(
                            (byte)(((int)img_P1.Red * (1 - img_XFraction)) + ((int)img_P2.Red * img_XFraction)),
                            (byte)(((int)img_P1.Green * (1 - img_XFraction)) + ((int)img_P2.Green * img_XFraction)),
                            (byte)(((int)img_P1.Blue * (1 - img_XFraction)) + ((int)img_P2.Blue * img_XFraction)));
                        img_Z2 = new Entities.Pixel(
                            (byte)(((int)img_P3.Red * (1 - img_XFraction)) + ((int)img_P4.Red * img_XFraction)),
                            (byte)(((int)img_P3.Green * (1 - img_XFraction)) + ((int)img_P4.Green * img_XFraction)),
                            (byte)(((int)img_P3.Blue * (1 - img_XFraction)) + ((int)img_P4.Blue * img_XFraction)));
                        img_NewPixel = new Entities.Pixel(
                            (byte)(((int)img_Z1.Red * (1 - img_YFraction)) + ((int)img_Z2.Red * img_YFraction)),
                            (byte)(((int)img_Z1.Green * (1 - img_YFraction)) + ((int)img_Z2.Green * img_YFraction)),
                            (byte)(((int)img_Z1.Blue * (1 - img_YFraction)) + ((int)img_Z2.Blue * img_YFraction)));
                        img_NewBuffer[j, i] = img_NewPixel;
                    }
                }

                return img_NewBuffer;
            }
            catch (Exception img_ex)
            {
                Logger.LogException(img_ex);
                throw;
            }
        }
    }
}
